<template>
  <el-dialog title="创建合同" :visible.sync="dialogVisible" @close="close" :close-on-click-modal="false" width="1200px">
    <div class="" v-loading="loading">
      <div class="order_box" v-if="form.yewuType == 2 && orderInfo && showOrder">
        <el-descriptions title="订单信息" :column="2" border>
          <el-descriptions-item label="游戏">{{ orderInfo.gameName }}</el-descriptions-item>
          <el-descriptions-item label="订单编号">{{ orderInfo.no }}</el-descriptions-item>
          <el-descriptions-item label="游戏账号">{{ orderInfo.accountNumber }}</el-descriptions-item>
          <el-descriptions-item label="卖家昵称">{{ orderInfo.sellerName }}</el-descriptions-item>
          <el-descriptions-item label="回收价">{{ orderInfo.backAmount }}</el-descriptions-item>
          <el-descriptions-item label="包赔费">{{ orderInfo.guaranteeAmount || '--' }}</el-descriptions-item>
          <el-descriptions-item label="总金额">{{ orderInfo.buyerActualAmount }}</el-descriptions-item>
          <el-descriptions-item label="卖家姓名" v-if="guaranteeInfo">{{ guaranteeInfo.name }}</el-descriptions-item>
          <el-descriptions-item label="卖家身份证" v-if="guaranteeInfo">{{ guaranteeInfo.idCard }}</el-descriptions-item>
          <el-descriptions-item label="卖家电话" v-if="guaranteeInfo">{{ guaranteeInfo.phoneNumber }}</el-descriptions-item>
          <el-descriptions-item label="卖家地址" v-if="guaranteeInfo">{{ guaranteeInfo.adress }}</el-descriptions-item>
        </el-descriptions>
        <i @click="closeOrder">
          <i class="el-icon-close"></i>
        </i>
      </div>

      <div class="order_box" v-if="form.yewuType == 3 && orderInfo && showOrder">
        <el-descriptions title="订单信息" :column="2" border>
          <el-descriptions-item label="游戏">{{ orderInfo.gameName }}</el-descriptions-item>
          <el-descriptions-item label="订单编号">{{ orderInfo.orderNo }}</el-descriptions-item>
          <el-descriptions-item label="游戏账号">{{ orderInfo.account }}</el-descriptions-item>
          <el-descriptions-item label="卖家昵称">{{ orderInfo.sellerNickname }}</el-descriptions-item>
          <el-descriptions-item label="包赔费">{{ orderInfo.compensationFee || '--' }}</el-descriptions-item>
          <el-descriptions-item label="总金额">{{ orderInfo.totalAmount }}</el-descriptions-item>
          <el-descriptions-item label="卖家姓名" v-if="guaranteeInfo">{{ guaranteeInfo.name }}</el-descriptions-item>
          <el-descriptions-item label="卖家身份证" v-if="guaranteeInfo">{{ guaranteeInfo.idCard }}</el-descriptions-item>
          <el-descriptions-item label="卖家电话" v-if="guaranteeInfo">{{ guaranteeInfo.phoneNumber }}</el-descriptions-item>
          <el-descriptions-item label="卖家地址" v-if="guaranteeInfo">{{ guaranteeInfo.adress }}</el-descriptions-item>
        </el-descriptions>
        <i @click="closeOrder">
          <i class="el-icon-close"></i>
        </i>
      </div>

      <div style="margin-bottom: 15px;margin-left: 30px;">
        <el-radio-group @change="startChange" v-model="form.startType">
          <el-radio-button :label="1">从模板发起</el-radio-button>
          <el-radio-button :label="2">从文件发起</el-radio-button>
        </el-radio-group>
      </div>
      <div style="color: #999;margin-bottom: 10px;margin-left: 26px;" class="tip">如有自己的合同文件，可选择“从文件发起”！</div>
      <el-form ref="form" :model="form" :rules="rules" :inline="true" label-width="100px">
        <el-form-item label="合同名称" prop="contractName">
          <el-input v-model="form.contractName" placeholder="请输入合同名称"></el-input>
        </el-form-item>

        <el-form-item label="模板类型" prop="bussinessType" v-if="form.startType == 1">
          <el-select @change="bussinessTypeChange" v-model="form.bussinessType" placeholder="请选择模板类型">
            <el-option v-for="dict in typeList" :key="dict.value" :label="dict.name" :value="dict.value" />
          </el-select>
          <span class="link" style="margin-left: 10px;" @click="yulan" v-if="form.bussinessType">预览</span>
        </el-form-item>
        <div class="tit-box flex alc">
          <div class="tit flex alc">
            <span style="width: 2px;height: 17px;background-color: #6C5DD3;margin-right: 3px;"></span>
            <b class="font16">签署方信息</b>
          </div>
          <div style="margin: 0 10px;" v-if="form.startType == 2">
            <el-button icon="el-icon-circle-plus-outline" type="primary" size="mini" @click="addQianshufang"
              v-loading="loading">添加签署方</el-button>
          </div>
          <div style="color: red;margin-left: 10px;" class="tip">签署方信息必须填写真实信息，合同签署过程中会进行实名校验。</div>
          <span @click="openOrder" v-if="(form.yewuType == 2 || form.yewuType == 3) && orderInfo && !showOrder"
            style="color: cornflowerblue;margin-left: 10px; cursor: pointer;user-select: none;">显示订单</span>
          <span @click="closeOrder" v-if="(form.yewuType == 2 || form.yewuType == 3) && orderInfo && showOrder"
            style="color: cornflowerblue;margin-left: 10px; cursor: pointer;user-select: none;">隐藏订单</span>
        </div>
        <div class="qianshufang_box flex">

          <div class="qianshufang_item" v-for="(item, i) in qianshufangList" :key="i">
            <div class="center">
              <b v-if="form.startType == 2">签署方{{ i + 1 }}</b>
              <b v-if="form.startType == 1 && i == 0">甲方
                <span v-if="form.bussinessType == 2">(买方)</span>
                <span v-if="form.bussinessType == 3">(转让方)</span>
              </b>
              <b v-if="form.startType == 1 && i == 1">乙方
                <span v-if="form.bussinessType == 2">(卖方)</span>
                <span v-if="form.bussinessType == 3">(收购方)</span>
              </b>
              <b v-if="form.startType == 1 && i == 2">丙方</b>
            </div>
            <div class="center" style="margin: 15px 0;">
              <el-button style="margin-left:15px" type="primary" size="mini" @click="openShopInfoSelect(i)"
                icon="el-icon-edit-outline" plain>快速填充</el-button>
              <el-button style="margin-left:15px" type="warning" size="mini" @click="clearInput(i)"
                icon="el-icon-delete" plain>清空</el-button>
              <el-button v-if="form.startType == 2" type="danger" style="margin-left:15px" size="mini"
                @click="removeRule(i)" icon="el-icon-remove-outline" plain>移除</el-button>
            </div>
            <div style="text-align: center;margin-bottom: 15px;">
              <!-- <span class="inputlabel">类型：</span> -->
              <el-radio-group @change="qianshufangChange($event, i)" v-model="item.type">
                <el-radio :label="2">个人</el-radio>
                <el-radio :label="3">企业</el-radio>
              </el-radio-group>
            </div>
            <div v-if="item.type == 2">
              <div class="flex inputrow">
                <span class="inputlabel"><span class="red">*</span> 姓名：</span>
                <el-input v-model="item.name" @change="qianshufangChange($event, i)"></el-input>
              </div>
              <div class="flex inputrow">
                <span class="inputlabel"><span class="red">*</span>身份证号：</span>
                <el-input v-model="item.idCard" @change="qianshufangChange($event, i)"></el-input>
              </div>
              <div class="flex inputrow">
                <span class="inputlabel"><span class="red">*</span>手机号：</span>
                <el-input v-model="item.phone" @change="qianshufangChange($event, i)"></el-input>
              </div>
              <div class="flex inputrow">
                <span class="inputlabel"><span class="red">*</span>联系地址：</span>
                <el-input v-model="item.address" @change="qianshufangChange($event, i)"></el-input>
              </div>

              <div class="flex inputrow">
                <span class="inputlabel"><span class="red">*</span>用户标识：</span>
                <el-radio-group v-model="item.linshi">
                  <el-radio :label="1">默认</el-radio>
                  <el-radio :label="2">临时</el-radio>
                </el-radio-group>
              </div>
            </div>
            <div class="qiyebox" v-if="item.type == 3">
              <div class="flex inputrow">
                <span class="inputlabel"><span class="red">*</span>企业名称：</span>
                <el-input v-model="item.cropName" @change="qianshufangChange($event, i)"></el-input>
              </div>
              <div class="flex inputrow">
                <span class="inputlabel"><span class="red">*</span>统一社会信用代码：</span>
                <el-input v-model="item.identNo" @change="qianshufangChange($event, i)"></el-input>
              </div>
              <div class="flex inputrow">
                <span class="inputlabel"><span class="red">*</span>法人姓名：</span>
                <el-input v-model="item.name" @change="qianshufangChange($event, i)"></el-input>
              </div>
              <div class="flex inputrow">
                <span class="inputlabel"><span class="red">*</span>法人身份证号：</span>
                <el-input v-model="item.idCard" @change="qianshufangChange($event, i)"></el-input>
              </div>
              <div class="flex inputrow">
                <span class="inputlabel"><span class="red">*</span>手机号：</span>
                <el-input v-model="item.phone" @change="qianshufangChange($event, i)"></el-input>
              </div>
              <div class="flex inputrow">
                <span class="inputlabel"><span class="red">*</span>联系地址：</span>
                <el-input v-model="item.address" @change="qianshufangChange($event, i)"></el-input>
              </div>
            </div>

          </div>

        </div>

      </el-form>
      <!-- 模版内容填写 -->
      <template v-if="form.startType == 1 && form.bussinessType">
        <el-form ref="form2" :model="fillData" :rules="rules2" :inline="true" label-width="100px">
          <el-form-item v-for="item in templateKeys[form.bussinessType]" :key="item.key" :label="item.label"
            :prop="item.key">
            <el-input @change="inputChange($event, form.bussinessType, item.key)" v-model="fillData[item.key]"
              :placeholder="item.placeholder || ''"></el-input>
          </el-form-item>
        </el-form>

      </template>
      <!-- 模版内容结束 -->
      <div class="tit-box flex alc" v-if="form.startType == 2">
        <div class="tit flex alc">
          <span style="width: 2px;height: 17px;background-color: #6C5DD3;margin-right: 3px;"></span>
          <b class="font16">合同文件</b>
        </div>
      </div>
      <div style="padding-left: 35px;" v-if="form.startType == 2" :class="{ hideUpload: this.file && this.file != '' }">
        <el-upload :file-list="fileList" :on-remove="fileRemove" ref="upload" :limit="1" accept=".doc, .docx, .pdf"
          :http-request="getFile" action="" :auto-upload="true" drag style="margin-bottom: 15px">
          <i style="color: #6C5DD3;margin-top: 20px;" class="el-icon-upload"></i>
          <div class="el-upload__text">
            <div>将文件拖到此处，或
              <em>点击上传</em>
            </div>
            <div>仅允许doc、docx、pdf格式文件。</div>
          </div>
        </el-upload>
      </div>

      <div class="center">
        <el-button @click="close()">取消</el-button>
        <el-button type="primary" @click="submitForm('form')" v-loading="loading">提交</el-button>
      </div>
    </div>
    <el-dialog title="选择店铺认证信息" :visible.sync="shopInfoVisible" append-to-body :close-on-click-modal="false"
      width="500px">
      <div class="">
        <el-form ref="form33" label-width="100px" class="">
          <el-form-item label="认证类型" prop="">
            <el-select v-model="selectAuthType" placeholder="请选择模板类型">
              <el-option label="个人认证" :value="2" />
              <el-option label="企业认证" :value="3" />
            </el-select>
          </el-form-item>
          <div class="center">
            <el-button @click="shopInfoVisible = false">取消</el-button>
            <el-button type="primary" @click="sureShopInfo" v-loading="loading">确认</el-button>
          </div>
        </el-form>
      </div>
    </el-dialog>
  </el-dialog>
</template>

<script>
import { createContract } from "@/api/interOrder/index";

import fadadaApi from "../../api/B2Cmanage/fadada";
import { convertCurrencyToChinese } from "../../utils/ruoyi";
export default {
  data() {
    return {
      showOrder: false,
      shopInfoVisible: false,
      dialogVisible: false,
      selectAuthType: 3,
      loading: false,
      typeList: [
        {
          name: "游戏账号交易合同",
          value: "2",
        },
        {
          name: "转让协议",
          value: "3",
        },
      ],
      qianshufangList: [
        {
          signName: "甲方",
          type: 2, //2个人 3企业
          name: "",
          idCard: "",
          phone: "",
          cropName: "",
          identNo: "",
          linshi: 1
        },
        {
          signName: "乙方",
          type: 2, //2个人 3企业
          name: "",
          idCard: "",
          phone: "",
          cropName: "",
          identNo: "",
          linshi: 1
        },
      ],
      // 快速填充的签署人index
      quickQianshuIndex: "",
      templateKeys: {
        2: [
          {
            key: "key1",
            label: "甲方",
            placeholder: "请输入甲方名称",
          },
          {
            key: "key2",
            label: "甲方证件号",
            placeholder: "请输入甲方证件号/统一社会信用代码",
          },
          {
            key: "key3",
            label: "乙方",
            placeholder: "请输入乙方名称",
          },
          {
            key: "key4",
            label: "乙方证件号",
            placeholder: "请输入乙方证件号/统一社会信用代码",
          },
          {
            key: "key5",
            label: "游戏名称",
            placeholder: "请输入交易游戏名(如：王者荣耀)",
          },
          {
            key: "key6",
            label: "游戏帐号",
            placeholder: "请输入交易的游戏账号",
          },
          {
            key: "key7",
            label: "区服",
            placeholder: "请输入交易的游戏区服",
          },
          {
            key: "key8",
            label: "人民币小写",
            placeholder: "请输入交易金额小写",
          },
          {
            key: "key9",
            label: "人民币大写",
            placeholder: "请输入交易金额大写",
          },
          {
            key: "key10",
            label: "收款信息",
            placeholder: "请输入卖家收款信息（支付宝银行卡等）",
          },
        ],
        3: [
          {
            key: "key1",
            label: "甲方",
            placeholder: "请输入甲方名称",
          },
          {
            key: "key2",
            label: "甲方证件号",
            placeholder: "请输入甲方证件号/统一社会信用代码",
          },
          {
            key: "key3",
            label: "甲方电话",
            placeholder: "请输入甲方联系电话",
          },
          {
            key: "key4",
            label: "甲方地址",
            placeholder: "请输入甲方联系地址",
          },
          {
            key: "key5",
            label: "乙方",
            placeholder: "请输入乙方名称",
          },
          {
            key: "key6",
            label: "乙方证件号",
            placeholder: "请输入乙方证件号/统一社会信用代码",
          },
          {
            key: "key7",
            label: "乙方法人",
            placeholder: "请输入乙方法定代表人",
          },
          {
            key: "key8",
            label: "乙方地址",
            placeholder: "请输入乙方联系地址",
          },
          {
            key: "key9",
            label: "乙方电话",
            placeholder: "请输入乙方联系电话",
          },

          {
            key: "key10",
            label: "游戏帐号",
            placeholder: "请输入交易的游戏账号",
          },
          {
            key: "key11",
            label: "账号ID",
            placeholder: "请输入交易的账号ID",
          },
          {
            key: "key12",
            label: "原密保手机",
            placeholder: "请输入原密保手机",
          },
          {
            key: "key13",
            label: "原注册邮箱",
            placeholder: "请输入原注册邮箱",
          },
          {
            key: "key14",
            label: "人民币小写",
            placeholder: "请输入交易金额小写",
          },
          {
            key: "key15",
            label: "人民币大写",
            placeholder: "请输入交易金额大写",
          },
        ],
      },

      fileList: [],
      // 表单参数
      form: {
        startType: 1,
        bussinessType: "",
        contractName: "",
        yewuType: '',
        bussinessId: ''
      },
      fillData: {
        key1: "",
        key2: "",
        key3: "",
        key4: "",
        key5: "",
        key6: "",
        key7: "",
        key8: "",
        key9: "",
        key10: "",
        key11: "",
        key12: "",
        key13: "",
        key14: "",
        key15: "",
      },
      file: "",

      rules: {
        contractName: [{ required: true, message: "请输入", trigger: "blur" }],
        bussinessType: [{ required: true, message: "请输入", trigger: "blur" }],
      },
      rules2: {
        key1: [{ required: true, message: "请输入", trigger: "blur" }],
        key2: [{ required: true, message: "请输入", trigger: "blur" }],
        key3: [{ required: true, message: "请输入", trigger: "blur" }],
        key4: [{ required: true, message: "请输入", trigger: "blur" }],
        key5: [{ required: true, message: "请输入", trigger: "blur" }],
        key6: [{ required: true, message: "请输入", trigger: "blur" }],
        key7: [{ required: true, message: "请输入", trigger: "blur" }],
        key8: [{ required: true, message: "请输入", trigger: "blur" }],
        key9: [{ required: true, message: "请输入", trigger: "blur" }],
        key10: [{ required: true, message: "请输入", trigger: "blur" }],
        key11: [{ required: true, message: "请输入", trigger: "blur" }],
        key12: [{ required: true, message: "请输入", trigger: "blur" }],
        key13: [{ required: true, message: "请输入", trigger: "blur" }],
        key14: [{ required: true, message: "请输入", trigger: "blur" }],
        key15: [{ required: true, message: "请输入", trigger: "blur" }],
      },
      orderInfo: null,//从订单打开，带过来的订单信息
      guaranteeInfo: null,//包赔信息
    };
  },
  computed: {
    shopInfo() {
      return this.$store.getters.shopInfo;
    },
  },
  created() {
    // this.headers = {
    //   token:'Bearer ' + getToken() ,
    //   Authorization:'Bearer ' + getToken()
    // }
    // this.uploadImgUrl = process.env.VUE_APP_BASE_API + "/upload/app";
  },
  methods: {
    // 打开弹窗
    open(order, type) {
      this.orderInfo = null
      this.guaranteeInfo = null
      this.fileList = [];
      this.file = "";
      if (order) {
        this.orderInfo = order
        this.form.bussinessId = this.orderInfo.id
        if (this.orderInfo.guaranteeInfo) {
          this.guaranteeInfo = JSON.parse(this.orderInfo.guaranteeInfo)
        }
        if (this.orderInfo.compensationMaterials) {
          this.guaranteeInfo = JSON.parse(this.orderInfo.compensationMaterials)
        }
        this.showOrder = true
        console.log("%c Line:402 🍿 this.orderInfo", "color:#42b983", this.orderInfo);
      }
      if (type) {
        console.log("%c Line:417 🧀 type", "color:#f5ce50", type);
        this.form.yewuType = type
      }
      // this.form = {
      //   startType: 1,
      //   bussinessType: "",
      //   contractName: "",
      // };
      this.resetQianshufang();

      this.dialogVisible = true;
    },
    closeOrder() {
      this.showOrder = false
    },
    openOrder() {
      this.showOrder = true
    },
    // 预览合同
    yulan() {
      const type = this.form.bussinessType
      if (type == 2) {
        // 游戏账号交易合同
        window.open('https://callback.asign.cn/web/short/JNBjMb527159', 'yulanhetong')
      } else if (type == 3) {
        // 转让协议
        window.open('https://callback.asign.cn/web/short/BZvA7f825016', 'yulanhetong')
      }
    },
    // 初始化签署方信息
    resetQianshufang() {
      this.qianshufangList = [
        {
          signName: "甲方",
          type: 2, //2个人 3企业
          name: "",
          idCard: "",
          phone: "",
          cropName: "",
          identNo: "",
          linshi: 1
        },
        {
          signName: "乙方",
          type: 2, //2个人 3企业
          name: "",
          idCard: "",
          phone: "",
          cropName: "",
          identNo: "",
          linshi: 1
        },
      ];
    },
    // 重置
    reset() {
      this.$refs.form.resetFields();
    },
    // 发起方式改变
    startChange(w) {
      this.fileList = [];
      this.file = "";
      this.resetQianshufang();
      this.fillData = {
        key1: "",
        key2: "",
        key3: "",
        key4: "",
        key5: "",
        key6: "",
        key7: "",
        key8: "",
        key9: "",
        key10: "",
        key11: "",
        key12: "",
        key13: "",
        key14: "",
        key15: "",
      };

      this.form.bussinessType = "";
    },
    bussinessTypeChange() {
      this.resetQianshufang();
      this.fillData = {
        key1: "",
        key2: "",
        key3: "",
        key4: "",
        key5: "",
        key6: "",
        key7: "",
        key8: "",
        key9: "",
        key10: "",
        key11: "",
        key12: "",
        key13: "",
        key14: "",
        key15: "",
      };
    },
    // 填写签署方信息输入框改变
    qianshufangChange(e, i, key) {
      this.asyncTemplate(i);
    },
    // 清空填写的签署人信息
    clearInput(i) {
      if (this.qianshufangList[i]) {
        const item = this.qianshufangList[i];
        item.name = "";
        item.idCard = "";
        item.phone = "";
        item.cropName = "";
        item.identNo = "";
        item.address = "";
        this.asyncTemplate(i);
      }
    },
    // 将签署 方信息 同步到模版中
    asyncTemplate(i) {
      if (this.qianshufangList[i]) {
        const item = this.qianshufangList[i];
        if (this.form.startType == 1 && this.form.bussinessType && item) {
          if (this.form.bussinessType == 2) {
            // 同步到模板内容中
            if (i === 0) {
              //签署人1 甲方
              this.fillData.key1 = item.cropName || item.name;
              this.fillData.key2 = item.identNo || item.idCard;
            }
            if (i === 1) {
              //签署人2 乙方
              this.fillData.key3 = item.cropName || item.name;
              this.fillData.key4 = item.identNo || item.idCard;
            }
          } else if (this.form.bussinessType == 3) {
            // 同步到模板内容中
            if (i === 0) {
              //签署人1 甲方
              this.fillData.key1 = item.cropName || item.name;
              this.fillData.key2 = item.identNo || item.idCard;
              this.fillData.key3 = item.phone || '';
              this.fillData.key4 = item.address || '';
            }
            if (i === 1) {
              //签署人2 乙方
              this.fillData.key5 = item.cropName || item.name;
              this.fillData.key6 = item.identNo || item.idCard;
              this.fillData.key7 = item.name || '';
              this.fillData.key8 = item.address || '';
              this.fillData.key9 = item.phone || '';
            }
          }
        }
      }
    },
    // 快速填充签署方信息
    openShopInfoSelect(i) {
      this.quickQianshuIndex = i;
      this.selectAuthType = "";
      if (this.shopInfo.authType == 2 || this.shopInfo.authType == 3) {
        // 只认证了一种，直接填充
        this.selectAuthType = this.shopInfo.authType;
        this.sureShopInfo();
      } else if (this.shopInfo.authType == 6) {
        // 认证了两种，弹出选择
        this.shopInfoVisible = true;
      } else {
        return this.$message.error("你的店铺还未认证，请前往“首页”进行认证！");
      }
    },
    // 确认店铺认证信息
    sureShopInfo() {
      if (!this.selectAuthType) {
        return this.$message.error("请先选择认证类型!");
      }
      this.shopInfoVisible = false;
      if (this.qianshufangList[this.quickQianshuIndex]) {
        const item = this.qianshufangList[this.quickQianshuIndex];
        if (this.selectAuthType == 3) {
          // 选用企业信息
          const corpInfo = JSON.parse(this.shopInfo.corpInfo);
          console.log("%c Line:515 🍩 corpInfo", "color:#33a5ff", corpInfo);
          item.type = 3;
          item.name = corpInfo.name;
          item.idCard = corpInfo.idCard;
          item.phone = corpInfo.phone;
          item.cropName = corpInfo.cropName;
          item.identNo = corpInfo.identNo;
          item.address = corpInfo.address;
        } else {
          // 选用个人认证信息
          const personInfo = JSON.parse(this.shopInfo.personInfo);
          item.type = 2;
          item.name = personInfo.name;
          item.idCard = personInfo.idCard;
          item.phone = personInfo.phone;
          item.cropName = "";
          item.identNo = "";
        }
        this.asyncTemplate(this.quickQianshuIndex);
        // if (this.form.startType == 1 && this.form.bussinessType && item) {
        //   // 同步到模板内容中
        //   if (this.quickQianshuIndex === 0) {
        //     //签署人1 甲方
        //     this.fillData.key1 = item.cropName || item.name;
        //     this.fillData.key2 = item.identNo || item.idCard;
        //   }
        //   if (this.quickQianshuIndex === 1) {
        //     //签署人2 乙方
        //     this.fillData.key3 = item.cropName || item.name;
        //     this.fillData.key4 = item.identNo || item.idCard;
        //   }
        // }
      }
    },
    // 添加签署方
    addQianshufang() {
      if (this.qianshufangList.length > 3) {
        return this.$message.error("最多只能添加4个签署方！");
      }
      this.qianshufangList.push({
        signName: "",
        type: 2, //2个人 3企业
        name: "",
        idCard: "",
        phone: "",
        cropName: "",
        identNo: "",
        linshi: 1
      });
    },
    // 移除签署方
    //移除一条规则
    removeRule(i) {
      if (this.qianshufangList.length <= 2) {
        return this.$message.error("最少要有两个签署方！");
      }
      this.qianshufangList.splice(i, 1);
    },
    // 获取文件
    async getFile(file) {
      // console.log("%c Line:94 🍯 file", "color:#7f2b82", file);
      //上传到后台
      file.code = 200;
      this.createForFile(file.file);
      return file;
    },
    // 移除文件
    fileRemove() {
      this.file = "";
      this.fileList = [];
    },
    // // 获取文件
    // getFile(file) {
    //   this.createForFile(file);
    // },
    // 从合同文件发起合同
    createForFile(file) {
      this.file = file;
    },
    // 字段填写变化
    inputChange(e, bussinessType, key) {
      if (
        (bussinessType == 2 && key == "key8") ||
        (bussinessType == 3 && key == "key14")
      ) {
        // 人民币小写改变
        // 自动生成人民币大写
        if (bussinessType == 2) {
          // 回收合同
          const daxie = this.getDaxie(e)
          this.fillData["key9"] = daxie || '';
        } else if (bussinessType == 3) {
          // 长明回收合同
          const daxie = this.getDaxie(e)
          this.fillData["key15"] = daxie || '';
        }
      }
    },
    getDaxie(e) {
      const daxie = convertCurrencyToChinese(e);
      if (daxie == 'a') {
        this.$message.error('金额格式不正确！')
        return ''
      } else if (daxie == 'b') {
        this.$message.error('金额数值过大！')
        return ''
      } else {
        return daxie
      }
    },
    submitForm(formName) {
      this.$refs["" + formName].validate(async (valid) => {
        if (valid) {
          // 校验表单
          if (this.form.startType == 1 && this.form.bussinessType) {
            this.$refs["form2"].validate(async (valid2) => {
              if (valid2) {
                this.submitFormSure();
              } else {
                this.$alert("请填写完整！");
              }
            });
          } else {
            this.submitFormSure();
          }
        } else {
          this.$alert("请填写完整！");
        }
      });
    },
    submitFormSure() {
      let isCheck = true;
      if (!this.form.contractName) {
        isCheck = false;
        return this.$message.error(`请填写合同名称！`);
      }
      if (this.form.startType == 1 && !this.form.bussinessType) {
        isCheck = false;
        return this.$message.error(`请选择合同模板！`);
      }
      if (this.form.startType == 2 && !this.file) {
        isCheck = false;
        return this.$message.error(`请选择合同文件！`);
      }
      if (this.form.startType == 1) {
        if (
          !this.fillData.key1 ||
          !this.fillData.key2 ||
          !this.fillData.key3 ||
          !this.fillData.key4 ||
          !this.fillData.key5 ||
          !this.fillData.key6 ||
          !this.fillData.key7 ||
          !this.fillData.key8 ||
          !this.fillData.key9 ||
          !this.fillData.key10
        ) {
          isCheck = false;
          this.$message.error("请将内容填写完整！");
        }
      }

      this.qianshufangList.forEach((r, i) => {
        if (r.type == 2) {
          // 个人
          if (!r.name || !r.idCard || !r.phone) {
            isCheck = false;
            this.$message.error("请将内容填写完整！");
          } else {
            if (!this.phoneReg.test(r.phone)) {
              isCheck = false;
              return this.$message.error(`签署人${i + 1}手机号格式不正确！`);
            }
            if (!this.idCardReg.test(r.idCard)) {
              isCheck = false;
              return this.$message.error(`签署人${i + 1}证件号格式不正确！`);
            }
          }
        } else {
          if (!r.name || !r.idCard || !r.phone || !r.cropName || !r.identNo) {
            isCheck = false;
          } else {
            if (!this.phoneReg.test(r.phone)) {
              isCheck = false;
              return this.$message.error(`签署人${i + 1}手机号格式不正确！`);
            }
            if (r.identNo.length != 18) {
              isCheck = false;
              return this.$message.error(`签署人${i + 1}证件号格式不正确！`);
            }
          }
        }
      });
      // isCheck;
      // console.log("%c Line:455 🌭 isCheck", "color:#e41a6a", isCheck);
      if (!isCheck) {
        return;
        this.$message.error("请将内容填写完整！");
      }

      this.form.fillData = { ...this.fillData };
      this.form.userSignList = [...this.qianshufangList];
    
      this.$confirm("提交前请确认您的信息填写正确，确定发起合同吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          this.loading = true;
          let res;
          if (this.form.startType == 1) {
            // 模板发起
            //中介订单
            if (this.form.yewuType == 3) {
               console.log(111)
              let formData = new FormData();
              formData.append("paramsReq", JSON.stringify(this.form));
              res = await createContract(formData);
            } else {
               console.log(222)
             res = await fadadaApi.createContractForT(this.form);
            }
          } else if (this.form.startType == 2) {
            // 文件发起
            let formData = new FormData();
            formData.append("file", this.file);
            formData.append("paramsReq", JSON.stringify(this.form));
            if (this.form.yewuType == 3) {
               console.log(333)
             res = await createContract(formData)
            } else {
              console.log(444)
               res = await fadadaApi.createContractForCustom(formData);
            }
          }
          this.loading = false;
          if (res.code == 200) {
            this.$message({
              type: "success",
              message: "合同创建成功！",
            });
            this.close();
            this.$parent.getList();
            if (this.$parent.openView) this.$parent.openView(res.data);
          }
        })
        .catch(() => {
          this.loading = false;
        });
    },

    close() {
      this.resetForm("form");
      this.resetForm("form2");
      this.dialogVisible = false;
    },
  },
};
</script>
<style scoped lang="scss">
.order_box {
  width: 600px;
  padding: 20px;
  border-radius: 10px;
  background: #fff;
  box-shadow: 0 0 5px 0 rgba(0, 0, 0, 0.3);
  position: fixed;
  right: 20px;
  top: 140px;

  i {
    font-size: 26px;
    position: absolute;
    top: 10px;
    right: 20px;
    width: 26px;
    height: 26px;
    cursor: pointer;
  }
}

.qianshufang_box {
  margin-left: 35px;
  flex-wrap: wrap;

  .qianshufang_item {
    width: 338px;
    background: #f3f6ff;
    border-radius: 10px;
    border: 1px solid #e2e9ff;

    padding: 10px;
    margin-bottom: 10px;
    margin-right: 15px;

    .el-row {
      margin-bottom: 10px;
    }

    .inputrow {
      margin-bottom: 15px;
      align-items: center;
    }
  }
}

::v-deep .qianshufang_item .el-input__inner {
  border: none;
}

.inputlabel {
  display: inline-block;
  width: 80px;
  flex-shrink: 0;
  text-align: right;
}

.qiyebox .inputlabel {
  width: 135px;
}

.tit-box {
  margin-bottom: 15px;
  margin-left: 35px;
}

::v-deep .hideUpload {
  .el-upload--text {
    display: none;
  }
}

::v-deep .el-radio input[aria-hidden="true"] {
  display: none !important;
}

::v-deep .el-radio:focus:not(.is-focus):not(:active):not(.is-disabled) .el-radio__inner {
  box-shadow: none !important;
}

::v-deep .el-upload-list__item {
  width: 400px;
  padding: 10px 30px;
  background-color: rgba(108, 93, 211, 0.1);
}

::v-deep .el-form-item {
  margin-bottom: 15px;
}

::v-deep .el-radio-button__inner {
  background-color: #f5f7ff;
  border: 1px solid #e8ebff;
}

::v-deep .el-upload-dragger {
  background: #f3f6ff;
  border-radius: 10px;
  border: 2px dashed #b6abff;
  width: 260px;
}

.red {
  color: red;
}

::v-deep .el-dialog__body {
  padding-top: 20px !important;
  padding-bottom: 20px !important;
  max-height: calc(100vh - 150px) !important;
}

::v-deep .el-descriptions--medium.is-bordered .el-descriptions-item__cell {
  padding: 5px 10px;
}
</style>
